function t() {
  var t = !(arguments.length > 0 && void 0 !== arguments[0]) || arguments[0];
  h.drawImage(S.bsrc, 0, 0, u.data.canvasparam.width, u.data.canvasparam.height);
  for (e = 0; e < S.rangelist.length; e++) S.rangelist[e].src && 1 == S.rangelist[e].field_type && h.drawImage(S.rangelist[e].src, parseInt(S.rangelist[e].x1), parseInt(S.rangelist[e].y1), parseInt(S.rangelist[e].width), parseInt(S.rangelist[e].height));
  h.drawImage(S.src, 0, 0, u.data.canvasparam.width, u.data.canvasparam.height), h.setTextBaseline("top");
  for (e = 0; e < S.rangelist.length; e++) S.rangelist[e].content && 2 == S.rangelist[e].field_type ? (h.setFillStyle("#" + S.rangelist[e].color),
    h.setFontSize(S.rangelist[e].font_size), l(h, e, 1)) : y > 0 && S.rangelist[e].content && 4 == S.rangelist[e].field_type && (h.setFillStyle("#" + S.rangelist[e].color),
      h.setFontSize(S.rangelist[e].font_size), l(h, e, 1));
  if (t) for (var e = 0; e < S.rangelist.length; e++) 4 != S.rangelist[e].field_type && (h.setLineWidth(2),
    h.setLineDash([10, 10], 2), h.setStrokeStyle("red"), h.strokeRect(parseInt(S.rangelist[e].x1), parseInt(S.rangelist[e].y1), parseInt(S.rangelist[e].width), parseInt(S.rangelist[e].height)));
  v && (h.setGlobalAlpha(.7), h.setFillStyle("black"), h.fillRect(0, 0, u.data.canvasparam.width, u.data.canvasparam.height),
    h.setGlobalAlpha(1), h.drawImage("/image/poster/input.png", 20, 50, 280, 150)),
    h.draw();
}

function e() {
  u.setData({
    inputshow: !0
  }), v = !0, t();
}

function a(t, e, a, n, s, i) {
  var r = parseInt(a) + parseInt(s), l = parseInt(n) + parseInt(i);
  return a = parseInt(a), n = parseInt(n), a < t && t < r && n < e && e < l;
}

function n() {
  wx.showLoading({
    title: "请求中",
    mask: !0
  }), S.src && wx.downloadFile({
    url: S.src,
    success: function (e) {
      200 === e.statusCode && (S.src = e.tempFilePath, t(), wx.hideLoading());
    }
  }), S.bsrc && wx.downloadFile({
    url: S.bsrc,
    success: function (e) {
      200 === e.statusCode && (S.bsrc = e.tempFilePath, t());
    }
  });
  for (var e = 0; e < S.rangelist.length; e++) "qr_code" == S.rangelist[e].field_coding && S.rangelist[e].imgurl && function (e) {
    wx.downloadFile({
      url: x + S.rangelist[e].imgurl,
      success: function (a) {
        200 === a.statusCode && (S.rangelist[e].src = a.tempFilePath, u.setData({
          weixincodeshow: 1
        }), t());
      }
    });
  }(e);
}

function s() {
  f.drawImage(S.bsrc, 0, 0, 3 * u.data.canvasparam.width, 3 * u.data.canvasparam.height);
  for (t = 0; t < S.rangelist.length; t++) S.rangelist[t].src && 1 == S.rangelist[t].field_type && f.drawImage(S.rangelist[t].src, 3 * parseInt(S.rangelist[t].x1), 3 * parseInt(S.rangelist[t].y1), 3 * parseInt(S.rangelist[t].width), 3 * parseInt(S.rangelist[t].height));
  f.drawImage(S.src, 0, 0, 3 * u.data.canvasparam.width, 3 * u.data.canvasparam.height),
    f.setTextBaseline("top");
  for (var t = 0; t < S.rangelist.length; t++) S.rangelist[t].content && 2 == S.rangelist[t].field_type ? (f.setFillStyle("#" + S.rangelist[t].color),
    f.setFontSize(3 * S.rangelist[t].font_size), l(f, t, 3)) : y > 0 && S.rangelist[t].content && 4 == S.rangelist[t].field_type && (f.setFillStyle("#" + S.rangelist[t].color),
      f.setFontSize(3 * S.rangelist[t].font_size), l(f, t, 3));
  f.draw(!1, i);
}

function i() {
  wx.canvasToTempFilePath({
    canvasId: "oCanvas",
    destWidth: 5 * I.width,
    destHeight: 5 * I.height,
    quality: 1,
    success: function (t) {
      var e = t.tempFilePath;
      _ > 0 ? wx.saveImageToPhotosAlbum({
        filePath: e,
        success: function (t) {
          wx.hideLoading(), wx.showToast({
            title: "海报已保存到相册",
            icon: "success",
            duration: 2e3
          }), m.getToken(function (t) {
            m.sendrequest("posterapi/addmnum", "POST", {
              token: t,
              id: c,
              m_id: o
            }, function (t) { });
          });
        }
      }) : (wx.hideLoading(), wx.previewImage({
        urls: [t.tempFilePath]
      }));
    }
  });
}

function r(t, e, a, n, s, i) {
  if (e) for (var r = e.length, l = 0; l < r; l++) {
    var g = e.substr(l, 1);
    t.fillText(g, a, n + s * l * i);
  }
}

function l(t, e, a) {
  var n = "center";
  switch (S.rangelist[e].font_align) {
    case "left":
      t.setTextAlign("left"), n = "left";
      break;

    case "right":
      t.setTextAlign("right"), n = "right";
      break;

    default:
      n = "center", t.setTextAlign("center");
  }
  "vertical" == S.rangelist[e].font_type ? "left" == n ? r(t, S.rangelist[e].content, parseInt(S.rangelist[e].x1) * a, parseInt(S.rangelist[e].y1) * a, S.rangelist[e].font_size, a) : "right" == n ? r(t, S.rangelist[e].content, (parseInt(S.rangelist[e].x1) + parseInt(S.rangelist[e].width)) * a, parseInt(S.rangelist[e].y1) * a, S.rangelist[e].font_size, a) : r(t, S.rangelist[e].content, (parseInt(S.rangelist[e].x1) + parseInt(S.rangelist[e].width) / 2) * a, parseInt(S.rangelist[e].y1) * a, S.rangelist[e].font_size, a) : "left" == n ? t.fillText(S.rangelist[e].content, (parseInt(S.rangelist[e].x1) + 1) * a, (parseInt(S.rangelist[e].y1) + 3) * a) : "right" == n ? t.fillText(S.rangelist[e].content, (parseInt(S.rangelist[e].x1) + parseInt(S.rangelist[e].width) + 1) * a, (parseInt(S.rangelist[e].y1) + 3) * a) : t.fillText(S.rangelist[e].content, (parseInt(S.rangelist[e].x1) + parseInt(S.rangelist[e].width) / 2 + 1) * a, (parseInt(S.rangelist[e].y1) + 3) * a);
}

function g() {
  w.drawImage(S.bsrc, 0, 0, 3 * u.data.canvasparam.width, 3 * u.data.canvasparam.height);
  for (t = 0; t < S.rangelist.length; t++) S.rangelist[t].src && 1 == S.rangelist[t].field_type && w.drawImage(S.rangelist[t].src, 3 * parseInt(S.rangelist[t].x1), 3 * parseInt(S.rangelist[t].y1), 3 * parseInt(S.rangelist[t].width), 3 * parseInt(S.rangelist[t].height));
  w.drawImage(S.src, 0, 0, 3 * u.data.canvasparam.width, 3 * u.data.canvasparam.height),
    w.setTextBaseline("top");
  for (var t = 0; t < S.rangelist.length; t++) S.rangelist[t].content && 2 == S.rangelist[t].field_type ? (w.setFillStyle("#" + S.rangelist[t].color),
    w.setFontSize(3 * S.rangelist[t].font_size), l(w, t, 3)) : y > 0 && S.rangelist[t].content && 4 == S.rangelist[t].field_type && (w.setFillStyle("#" + S.rangelist[t].color),
      w.setFontSize(3 * S.rangelist[t].font_size), l(w, t, 3));
  w.draw(!1, function () {
    wx.canvasToTempFilePath({
      canvasId: "upCanvas",
      destWidth: 3 * I.width,
      destHeight: 3 * I.height,
      fileType: "jpg",
      quality: 1,
      success: function (t) {
        var e = t.tempFilePath;
        m.getToken(function (t) {
          m.upload("posterapi/uploadshareimg", e, "shareimg", {
            token: t,
            m_id: o
          }, function (t) { });
        });
      }
    });
  });
}

var o, c, d, h, p, u, f, w, m = require("../../utils/util.js"), x = getApp().globalData.url.imgurl, I = {
  width: 0,
  height: 0
}, v = !1, y = 0, _ = 0, S = {
  bsrc: "",
  src: "",
  rangelist: []
};

Page({
  data: {
    canvasparam: I,
    imgurl: x,
    inputshow: !1,
    inputvalue: "",
    hideshowtip: !1,
    weixincodeshow: 0,
    openmark: y
  },
  onLoad: function (t) {
    var e = wx.getSystemInfoSync();
    this.setData({
      platform: e.platform
    }), v = !1, o = t.micro_id, c = t.log_id, u = this, m.getToken(function (t) {
      m.sendrequest("posterapi/getsetup", "POST", {
        token: t,
        id: o
      }, function (t) {
        1 == t.status && (S.src = x + t.data.background_path, S.bsrc = x + t.data.background_bottom,
          S.rangelist = t.data.setuplist, y = t.data.watermark, I.width = parseInt(t.data.background_width) / 2,
          I.height = parseInt(t.data.background_height) / 2, n(), u.setData({
            canvasparam: I,
            openmark: t.data.watermark
          }), d = t.data.uid);
      });
    }), f = wx.createCanvasContext("oCanvas"), h = wx.createCanvasContext("posterCanvas"),
      w = wx.createCanvasContext("upCanvas");
  },
  onShow: function () {
    try {
      var e = wx.getStorageSync("cutpath"), a = wx.getStorageSync("cutindex");
    } catch (t) { }
    e && (a || 0 == a) && (S.rangelist[p].src = e, wx.removeStorageSync("cutpath"),
      wx.removeStorageSync("cutindex"), t());
  },
  onShareAppMessage: function (t) {
    return {
      title: "小婷海报",
      path: "pages/share/share?uid=" + d + "&&mid=" + o,
      success: function (t) {
        g();
      },
      fail: function (t) { }
    };
  },
  preview: function (t) {
    wx.showLoading({
      title: "生成中",
      mask: !0
    }), _ = 0, s();
  },
  bindcreate: function (t) {
    wx.showLoading({
      title: "生成中",
      mask: !0
    }), _ = 1, s(), m.getToken(function (e) {
      m.sendrequest("posterapi/setfrom", "POST", {
        token: e,
        formid: t.detail.formId
      }, function (t) { });
    });
  },
  frontconfirm: function (e) {
    u.setData({
      inputshow: !1
    }), v = !1, S.rangelist[p].content = e.detail.value, t();
  },
  cancelwater: function () {
    wx.reLaunch({
      url: "/pages/center/center"
    });
  },
  clickrange: function (n) {
    if (v && a(n.touches[0].x, n.touches[0].y, 35, 50, 48, 48)) return u.setData({
      inputshow: !1
    }), v = !1, t(), !0;
    if (v && a(n.touches[0].x, n.touches[0].y, 245, 50, 50, 48)) return u.setData({
      inputvalue: ""
    }), !0;
    if (v) return !0;
    for (var s = !1, i = "", r = 0; r < S.rangelist.length; r++) if (a(n.touches[0].x, n.touches[0].y, S.rangelist[r].x1, S.rangelist[r].y1, S.rangelist[r].width, S.rangelist[r].height)) {
      if (4 == S.rangelist[r].field_type) return !0;
      s = !0, "" == i ? i = r : S.rangelist[r].sorting > S.rangelist[i].sorting && (i = r);
    }
    s && (1 == S.rangelist[i].field_type ? wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: function (t) {
        S.img = t.tempFilePaths, p = i, wx.navigateTo({
          url: "/pages/image/image?width=" + S.rangelist[i].width + "&height=" + S.rangelist[i].height + "&cutindex=" + i + "&path=" + t.tempFilePaths[0]
        });
      }
    }) : (p = i, this.setData({
      inputvalue: S.rangelist[i].content
    }), e()));
  }
});